<template>
	<view class="Home">
		<view class="bgc">
			<view class="backgound"></view>
			<view class="backgound2"></view>
		</view>
    <view class="container">
      <uni-section>
        <uni-notice-bar show-icon scrollable
                        :text="infor" />
      </uni-section>
    </view>
		<view class="top">
			<view style="display: flex; align-items: center; height: 100rpx;">
				<view class="img">
					<image src="../../static/forest.png" mode=""></image>
				</view>
				<view class="sign-left">
					<view class="name">{{form.name}}</view>
					<view class="phone">{{form.phone}}</view>
<!--          <view class="exdate">到期时间:{{form.exdate}}</view>-->
				</view>

			</view>
<!--			<view class="out" @click="quit()">退出登录</view>-->
		</view>
		<view class="data" v-if="state == 'ri'">
			<view class="data_index">
				<view class="data_bgc">{{Saleamount}}</view>
				<view class="data_text">今日销售</view>
			</view>
			<view class="data_index">
				<view class="data_bgc">{{Stockamount}}</view>
				<view class="data_text">今日进货</view>
			</view>
			<view class="data_index">
				<view class="data_bgc">{{Costamount}}</view>
				<view class="data_text">今日成本</view>
			</view>
			<view class="data_index" style="border-right: none;padding-right: 0px;">
				<view class="data_bgc">{{profit}}</view>
				<view class="data_text">今日利润</view>
			</view>
		</view>
    <view class="data" v-if="state == 'yue'">
      <view class="data_index">
        <view class="data_bgc">{{Saleamount}}</view>
        <view class="data_text">本月销售</view>
      </view>
      <view class="data_index">
        <view class="data_bgc">{{Stockamount}}</view>
        <view class="data_text">本月进货</view>
      </view>
      <view class="data_index">
        <view class="data_bgc">{{Costamount}}</view>
        <view class="data_text">本月成本</view>
      </view>
      <view class="data_index" style="border-right: none;padding-right: 0px;">
        <view class="data_bgc">{{profit}}</view>
        <view class="data_text">本月利润</view>
      </view>
    </view>
    <view class="data" v-if="state == 'nian'">
      <view class="data_index">
        <view class="data_bgc">{{Saleamount}}</view>
        <view class="data_text">本年销售</view>
      </view>
      <view class="data_index">
        <view class="data_bgc">{{Stockamount}}</view>
        <view class="data_text">本年进货</view>
      </view>
      <view class="data_index">
        <view class="data_bgc">{{Costamount}}</view>
        <view class="data_text">本年成本</view>
      </view>
      <view class="data_index" style="border-right: none;padding-right: 0px;">
        <view class="data_bgc">{{profit}}</view>
        <view class="data_text">本年利润</view>
      </view>
    </view>
		<view class="Refresh">
			<view class="Releft">常用功能</view>
      <view class="MI">
        <u-icon name="reload" color="#000" size="16"></u-icon>
        <view class="mini" @click="presa">日合计</view>
      </view>
      <view class="MI">
        <u-icon name="reload" color="#000" size="16"></u-icon>
        <view class="mini" @click="yuepresa">月合计</view>
      </view>
			<view class="MI">
				<u-icon name="reload" color="#000" size="16"></u-icon>
				<view class="mini" @click="nianpresa">年合计</view>
			</view>
		</view>
		<view class="info">
			<view class="info01">
				<view class="index01" @click="onclick('Stock')">
					<view class="">
						<view class="text01">进货单</view>
						<view class="border"></view>
					</view>
					<image src="@/static/Subtract.png" mode="" class="image01"></image>
				</view>
				<view class="index01" @click="onclick('Sale')">
					<view class="">
						<view class="text01">销货单</view>
						<view class="border"></view>
					</view>
					<image src="@/static/Subtract (1).png" mode="" class="image01"></image>
				</view>
			</view>
			<view class="index02" @click="onclick('Cost')">
				<view class="">
					<view class="text01">成本费用</view>
					<view class="border"></view>
				</view>
				<image src="@/static/Vector.png" mode="" class="image02"></image>
			</view>
      <view class="info01">
        <!--			<view class="info01" @click="onclick('Custom')">-->
        <view class="index01" @click="onclick('Custom')">
          <view class="">
            <view class="text01">客户明细</view>
            <view class="border"></view>
          </view>
          <image src="@/static/Vector (1).png" mode="" class="image01"></image>
        </view>
        <view class="index01" @click="onclick('Supplier')">
          <view class="">
            <view class="text01">供应商明细</view>
            <view class="border"></view>
          </view>
          <image src="@/static/Frame.png" mode="" class="image01"></image>
        </view>
      </view>
			<view class="info01">
				<view class="index01" @click="onclick('yj')">
					<view class="">
						<view class="text01">意见反馈</view>
						<view class="border"></view>
					</view>
					<image src="@/static/意见.png" mode="" class="image01"></image>
				</view>
        <view class="index01" @click="onclick('ces')">
          <view class="">
            <view class="text01">ces</view>
            <view class="border"></view>
          </view>
          <image src="@/static/意见.png" mode="" class="image01"></image>
        </view>
			</view>
			<view class="grid_20">
				<view class="grid1">
					技术支持：睿创软件技术
				</view>
				<view class="grid1">
					咨询电话：152 6353 0816
				</view>
			</view>
		</view>

	</view>

</template>

<script>
	import {
		getOpenid,
		quit,
		getamount,
		getyueamount,
    getuserinfor,
    getinfor,
		getnianamount
	} from '@/common/api.js';
	export default {
		data() {
			return {
        state: 'ri',
				Flag: true,
				infor: '加载中',
				form: {},
				name: '',
				exdate: '',
				Stockamount: '',
				Saleamount: '',
				Costamount: '',
				profit: '',
				phone: ''
			}
		},
		onLoad() {
			this.wxLogin()
			this.geuser()
			this.getamount()
			this.getinfor()
		},
		methods: {
			presa() {
        this.state = 'ri'
				this.getamount()
			},yuepresa() {
        this.state = 'yue'
				this.getyueamount()
			},nianpresa() {
        this.state = 'nian'
				this.getnianamount()
			},
			// Costqueryall,
      getinfor() {
        let params = {
          type: '首页公告'
        }
        uni.showLoading({
          title: '加载中...',
        });
        getinfor(params).then((res) => {
          uni.hideLoading();
          console.log(res)
          this.infor = res.data;
        })
      },
			getamount() {
				//获取当前日期 yyyymmdd
				let date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				if (month < 10) {
					month = '0' + month;
				}
				if (day < 10) {
					day = '0' + day;
				}
				let params = {
					companyId: uni.getStorageSync('companyId'),
					date: year + month + day
				}
				uni.showLoading({
					title: '加载中...',
				});
				getamount(params).then((res) => {
					uni.hideLoading();
					console.log(res)
					this.Stockamount = res.data.Stockamount;
					this.Saleamount = res.data.Saleamount;
					this.Costamount = res.data.Costamount;
					this.profit = res.data.profit;
				})
			},
      getyueamount() {
        //获取当前日期 yyyymmdd
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        if (month < 10) {
          month = '0' + month;
        }
        let params = {
          companyId: uni.getStorageSync('companyId'),
          date: year + month
        }
        uni.showLoading({
          title: '加载中...',
        });
        getyueamount(params).then((res) => {
          uni.hideLoading();
          console.log(res)
          this.Stockamount = res.data.Stockamount;
          this.Saleamount = res.data.Saleamount;
          this.Costamount = res.data.Costamount;
          this.profit = res.data.profit;
        })
      },
      getnianamount() {
        //获取当前日期 yyyymmdd
        let date = new Date();
        let year = date.getFullYear();
        let params = {
          companyId: uni.getStorageSync('companyId'),
          date: year
        }
        uni.showLoading({
          title: '加载中...',
        });
        getnianamount(params).then((res) => {
          uni.hideLoading();
          console.log(res)
          this.Stockamount = res.data.Stockamount;
          this.Saleamount = res.data.Saleamount;
          this.Costamount = res.data.Costamount;
          this.profit = res.data.profit;
        })
      },
			quit() {
				let from = {}
				from.openid = uni.getStorageSync('openid')
				from.account = uni.getStorageSync('account')
				quit(from).then((res) => {
					uni.showModal({
						content: '退出成功',
						showCancel: false,
						success(res) {

							// that.Flag = true;
						},
					});
					this.wxLogin()
				})
			},
      checkExpiry() {
        let params = {
          id: this.form.id,
        }
        uni.showLoading({
          title: '加载中...',
        });
        getuserinfor(params).then((res) => {
          uni.hideLoading();
          console.log(res)
          this.exdate = res.data.exdate;
        })
      },
			//微信登录
			wxLogin() {
				let that = this;
				uni.login({ //获取code
					onlyAuthorize: true,
					provider: 'weixin',
					success(loginRes) {
						const data = {
							code: loginRes.code
						};
						uni.showLoading({
							title: '微信登录中...',
						});
						getOpenid(data).then((res) => { //调用后端接口，根据code获取openid
							uni.hideLoading();
							that.Flag = false;
							console.log(res);
							uni.setStorageSync('openid', res.openid);

							if (res.code == 200) {
								that.flag = true;
								that.form = res.infor
								that.exdate = res.infor.exdate
								uni.setStorageSync('companyId', res.infor.companyId)
								uni.setStorageSync('infor', res.infor)
							} else if (res.code == 500) {
								console.log('ccc');
								uni.showModal({
									content: res.msg,
									showCancel: false,
									success(res) {
										uni.navigateTo({
											url: '/pages/login'
										})
										that.Flag = true;
									},
								});
							}
						})
					},
				});
			},
			geuser() {
				this.name = uni.getStorageSync('name')
				this.phone = uni.getStorageSync('phone')
			},
      formatDate(date) {
        const y = date.getFullYear();
        const m = String(date.getMonth() + 1).padStart(2, '0');
        const d = String(date.getDate()).padStart(2, '0');
        const hh = String(date.getHours()).padStart(2, '0');
        const mm = String(date.getMinutes()).padStart(2, '0');
        const ss = String(date.getSeconds()).padStart(2, '0');
        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
      },
			onclick(e) {
        this.checkExpiry();
        if (this.exdate == '' || this.exdate == null || this.exdate == undefined){
          this.checkExpiry();
        }
        const nowdate = this.formatDate(new Date());
        const exdate = this.exdate;
        console.log(nowdate);
        console.log(exdate);
        if (nowdate > exdate) {
          console.log('您的服务已过期，请续费！');
          uni.showModal({
            content: '您的服务已过期，请续费！',
            showCancel: false,
            success(res) {
            },
          });
          return false;
        } else {
          console.log('服务仍在有效期内');
        }
				if (e == 'yj') {
					uni.navigateTo({
						url: '/subPackage/Function/Price/MYD'
					})
				}
        if (e == 'ces') {
          uni.navigateTo({
            url: '/subPackage/Function/Price/ZHUCE'
          })
        }
        if (e == 'Stock') {
					uni.navigateTo({
						url: '/pages/Functions/index01'
					})
				}
				if (e == 'Sale') {
					uni.navigateTo({
						url: '/pages/Functions/Sale'
					})
				}
				if (e == 'Cost') {
					uni.navigateTo({
						url: '/pages/Functions/Cost'
					})
				}
				if (e == 'Custom') {
					uni.navigateTo({
						url: '/pages/Functions/Custom'
					})
				}
				if (e == 'Supplier') {
					uni.navigateTo({
						url: '/pages/Functions/Supplier'
					})
				}
				// uni.navigateTo({
				// 	url: '/pages/Functions/index01'
				// })
			}
		}
	}
</script>

<style>
	.Home {
		height: 100%;
		width: 100%;
	}

	.bgc {
		display: flex;
	}

	.backgound {
		width: 100%;
		height: 300rpx;
		background-image: linear-gradient(#FFE2D1, #FDF8F2);
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: -9999;
	}

	.backgound2 {
		width: 100%;
		height: 100%;
		background-color: #f0f0f0;
		position: absolute;
		top: 300rpx;
		z-index: -9999;
	}

	.top {
		border-radius: 30rpx;
		margin: 20rpx 30rpx 30rpx 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.sign-left {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		height: 100%;
		margin-left: 20rpx;
	}

	.name {
		font-size: 40rpx;
		/* color: white; */
		font-weight: bold;
	}

	.phone {
		font-size: 30rpx;
		/* color: white; */
	}
  .exdate {
		font-size: 22rpx;
    color: red;
    font-weight: bold;
		/* color: white; */
	}

	.sign-right {
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		box-sizing: border-box;
		align-items: flex-end;
	}

	.img {
		width: 100rpx;
		height: 100rpx;
	}

	.img image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.out {
		height: 60rpx;
		border-radius: 50rpx;
		padding: 10rpx 30rpx;
		box-sizing: border-box;
		background-color: red;
		color: white;
		font-weight: bold;
	}

	.info01 {
		display: flex;
		margin: 0 30rpx;
		justify-content: space-between;
    margin-top: 25rpx;
		/* flex-wrap: wrap; */
	}

	.index01 {
		width: 270rpx;
		height: 56rpx;
		padding: 30rpx;
		border-radius: 30rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;
	}

	.image01 {
		width: 54rpx;
		height: 56rpx;
		margin-top: 20rpx;
	}

	.text01 {
		font-size: 30rpx;
		/* margin-left: 25rpx; */
		font-weight: bold;
	}

	.border {
		width: 40rpx;
		height: 26rpx;
		border-bottom: 6rpx solid #F9363A;
    border-radius: 2px;
	}


	.index02 {
		width: 630rpx;
		height: 56rpx;
		margin: 30rpx;
		padding: 30rpx;
		border-radius: 30rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.image02 {
		width: 54rpx;
		height: 56rpx;
	}

  .grid1{
    font-size: 12px;
  }

	.data {
		background-color: white;
		border-radius: 30rpx;
		display: flex;
		margin: 30rpx;
		justify-content: space-around;
		height: 180rpx;
		align-items: center;
	}

	.data_index {
		text-align: center;
		line-height: 50rpx;
		height: 100rpx;
		border-right: 1px solid;
		padding-right: 20px;
	}

	.data_bgc {
		font-size: 30rpx;
		font-weight: bold;
	}

	.data_text {
		font-size: 25rpx;
		color: #737373;
	}

	.Refresh {
		display: flex;
		justify-content: space-between;
		margin: 30rpx;
	}

	.Releft {
		font-size: 38rpx;
		font-weight: bold;
	}

	.MI {
		display: flex;
		justify-content: space-between;
		align-items: center;
		border: 1rpx solid;
		padding: 6rpx 20rpx;
		border-radius: 20rpx;
		background-color: white;
	}

	.mini {
		margin-left: 5rpx;
		font-weight: bold;
    font-size: 26rpx;
	}

	.grid_20 {
		font-size: 15px;
		color: #787878 ;
		margin-top: 20rpx;
		text-align: center;
	}
  //.section_2 {
  //  background-color: rgba(255, 255, 255, 1);
  //  border-radius: 20rpx;
  //  width: 96%;
  //  height: 93rpx;
  //  display: flex;
  //  flex-direction: row;
  //  margin: 20rpx auto;
  //}
  //
  //.group_1 {
  //  width: 464rpx;
  //  height: 58rpx;
  //  margin: 18rpx 112rpx 0 25rpx;
  //  display: flex;
  //  flex-direction: column;
  //  justify-content: space-between;
  //}
  //
  //.image-text_2 {
  //  width: 464rpx;
  //  height: 20rpx;
  //  display: flex;
  //  flex-direction: row;
  //  justify-content: space-between;
  //}
  //
  //.thumbnail_1 {
  //  width: 7rpx;
  //  height: 8rpx;
  //  margin-top: 22rpx;
  //}
  //
  //.text-group_2 {
  //  width: 443rpx;
  //  height: 20rpx;
  //  overflow-wrap: break-word;
  //  color: rgba(51, 51, 51, 1);
  //  font-size: 26rpx;
  //  font-family: PingFangSC-Regular;
  //  font-weight: NaN;
  //  text-align: start;
  //  white-space: nowrap;
  //  line-height: 17rpx;
  //  margin-top: 20rpx;
  //}
  //.image_2 {
  //  width: 53rpx;
  //  height: 57rpx;
  //  margin: 19rpx 0 0 36rpx;
  //}
  .container {
    /* #ifndef APP-NVUE */
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    /* #endif */
  }
</style>
